package com.wbihua.grpc.demo.config;

import com.wbihua.grpc.demo.condition.LinuxCondtion;
import com.wbihua.grpc.demo.condition.WindowCondtion;
import com.wbihua.grpc.demo.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

@Configuration
@PropertySource(value = "classpath:/user.properties",encoding = "UTF-8")
public class UserConfig implements EmbeddedValueResolverAware {

    private StringValueResolver stringValueResolver;

    @Conditional(WindowCondtion.class)
    @Bean("zhansan")
    User user(@Value("${user.id}") Long id){
        String userName = stringValueResolver.resolveStringValue("${user.userName}");
        return new User(id,userName);
    }

    @Conditional(LinuxCondtion.class)
    @Bean("lisi")
    User user1(){
        return new User(2L,"lisi");
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
        this.stringValueResolver = stringValueResolver;
    }
}
